package com.gitee.cashzhang27.test.boot.activiti.controller;

import cn.hutool.core.util.IdUtil;
import com.google.common.collect.Maps;
import io.swagger.annotations.Api;
import java.util.Map;
import lombok.AllArgsConstructor;
import org.activiti.engine.HistoryService;
import org.activiti.engine.ProcessEngine;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.engine.repository.ProcessDefinition;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.activiti.engine.task.TaskQuery;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author Cash Zhang
 * @version v1.0
 * @since 2020/04/22 11:11
 */
@RestController
@AllArgsConstructor
@RequestMapping("/house-audit-flow")
@Api(value = "HouseAuditFlow", tags = "房源审核流程")
public class HouseAuditFlow {

  private ProcessEngine processEngine;

  private RepositoryService repositoryService;

  @GetMapping("create")
  public String create() {
    String createUser = "Kermit";
    String auditUser = "Alice";
    String businessKey = IdUtil.fastSimpleUUID();

    Map<String, Object> variables = Maps.newHashMap();
    variables.put("createUser", createUser);
    RuntimeService runtimeService = this.processEngine.getRuntimeService();
    ProcessDefinition processDefinition =
        this.repositoryService.createProcessDefinitionQuery().deploymentId("32523").singleResult();
    ProcessInstance processInstance =
        runtimeService.startProcessInstanceById(processDefinition.getId(), businessKey, variables);
    String processInstanceId = processInstance.getProcessInstanceId();
    TaskService taskService = this.processEngine.getTaskService();

    Task currentTask =
        taskService
            .createTaskQuery()
            .processInstanceId(processInstanceId)
            .taskAssignee(createUser)
            .singleResult();

    String taskId = currentTask.getId();

    variables.put("auditUser", auditUser);
    taskService.complete(taskId, variables);
    return businessKey;
  }

  @GetMapping("reCreate/{businessKey}")
  public void reCreate(@PathVariable("businessKey") String businessKey) {

    String auditUser = "Alice";
    HistoryService historyService = this.processEngine.getHistoryService();

    TaskService taskService = this.processEngine.getTaskService();
    Map<String, Object> variables = Maps.newHashMap();

    TaskQuery taskQuery = taskService.createTaskQuery().processInstanceBusinessKey(businessKey);
    String processInstanceId = taskQuery.singleResult().getProcessInstanceId();

    String createUser =
        (String)
            historyService
                .createHistoricVariableInstanceQuery()
                .processInstanceId(processInstanceId)
                .variableName("createUser")
                .list()
                .get(0)
                .getValue();

    Task currentTask = taskQuery.taskAssignee(createUser).singleResult();
    String taskId = currentTask.getId();

    variables.put("auditUser", auditUser);
    taskService.complete(taskId, variables);
  }

  @GetMapping("audit/{businessKey}")
  public void audit(
      @PathVariable("businessKey") String businessKey,
      @RequestParam("auditAgree") Boolean auditAgree) {

    String auditUser = "Alice";
    HistoryService historyService = this.processEngine.getHistoryService();

    TaskService taskService = this.processEngine.getTaskService();
    Map<String, Object> variables = Maps.newHashMap();

    TaskQuery taskQuery = taskService.createTaskQuery().processInstanceBusinessKey(businessKey);
    String processInstanceId = taskQuery.singleResult().getProcessInstanceId();

    String createUser =
        (String)
            historyService
                .createHistoricVariableInstanceQuery()
                .processInstanceId(processInstanceId)
                .variableName("createUser")
                .list()
                .get(0)
                .getValue();

    Task currentTask = taskQuery.taskAssignee(auditUser).singleResult();
    String taskId = currentTask.getId();
    variables.put("auditAgree", auditAgree);
    variables.put("creatUser", createUser);
    taskService.complete(taskId, variables);
  }

  @GetMapping("leased/{businessKey}")
  public void leased(
      @PathVariable("businessKey") String businessKey, @RequestParam("leased") Boolean leased) {
    String auditUser = "Alice";

    TaskService taskService = this.processEngine.getTaskService();
    TaskQuery taskQuery = taskService.createTaskQuery().processInstanceBusinessKey(businessKey);
    String processInstanceId = taskQuery.singleResult().getProcessInstanceId();

    HistoryService historyService = this.processEngine.getHistoryService();

    String createUser =
        (String)
            historyService
                .createHistoricVariableInstanceQuery()
                .processInstanceId(processInstanceId)
                .variableName("createUser")
                .list()
                .get(0)
                .getValue();

    Map<String, Object> variables = Maps.newHashMap();

    Task kermitTask = taskQuery.taskAssignee(createUser).singleResult();
    String taskId = kermitTask.getId();
    variables.put("leased", leased);
    if (!leased) {
      variables.put("auditUser", auditUser);
    }
    taskService.complete(taskId, variables);
  }
}
